package black.market.location;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import black.market.main.BlackPublic;
import black.market.main.BlackPublic.Action;

public class ReadLocation implements LocationListener {
	public static final String TAG = "ReadLocation";

	private String sProvider = null;
	private String geoMsg = null;
	private int sStatus = 0;
	private double lat = 0;
	private double lng = 0;
	private boolean isServiceState = false;

	private LocationManager locationManager = null;
	private Context mContext = null;

	public ReadLocation(Context context) {
		super();
		// TODO Auto-generated constructor stub
		this.mContext = context;
	}

	@Override
	protected void finalize() throws Throwable {
		// TODO Auto-generated method stub
		super.finalize();
	}

	public void startReadLocation(long time, float dis) {
		isServiceState = true;
		Log.d(TAG, "startReadLocation()");
		locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
		sProvider = locationManager.getBestProvider(new Criteria(), true);
		Log.d(TAG, "GetBestProvider: " + sProvider);
		locationManager.requestLocationUpdates(sProvider, time, dis, this);
	}

	public void stopReadLocation() {
		isServiceState = false;
		Log.d(TAG, "stopReadLocation()");
		locationManager.removeUpdates(this);
	}

	public boolean isbServiceState() {
		return isServiceState;
	}

	public void setbServiceState(boolean bServiceState) {
		this.isServiceState = bServiceState;
	}

	public int getsStatus() {
		return sStatus;
	}

	public void setsStatus(int sStatus) {
		this.sStatus = sStatus;
	}

	public LocationManager getLocationManager() {
		return locationManager;
	}

	public void setLocationManager(LocationManager locationManager) {
		this.locationManager = locationManager;
	}

	public String getsProvider() {
		return sProvider;
	}

	public void setsProvider(String sProvider) {
		this.sProvider = sProvider;
	}

	public String getGeoMsg() {
		return geoMsg;
	}

	public void setGeoMsg(String geoMsg) {
		this.geoMsg = geoMsg;
	}

	public double getLat() {
		return lat;
	}

	public void setLat(double lat) {
		this.lat = lat;
	}

	public double getLng() {
		return lng;
	}

	public void setLng(double lng) {
		this.lng = lng;
	}

	@Override
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		Log.d(TAG, "location changed: " + location.getTime());
		if (location.getLatitude() > 0 && location.getLatitude() > 0) {
			Log.d(TAG, "onLocationChanged: lat(" + location.getLatitude() + ")" + ", lng(" + location.getLongitude() + ")" + ", alt(" + location.getAltitude() + ")");
			BlackPublic.longToastMsg(mContext, "onLocationChanged: lat(" + location.getLatitude() + ")" + ", lng(" + location.getLongitude() + ")" + ", alt(" + location.getAltitude() + ")");
			ContentValues cv = new ContentValues();
			cv.put(MapContentProvider.MAP_LAT, Double.toString(location.getLatitude()));
			cv.put(MapContentProvider.MAP_LNG, Double.toString(location.getLongitude()));
			mContext.getContentResolver().insert(MapContentProvider.MAP_GEOPOINT_URI, cv);
			Intent intent = new Intent();
			intent.setAction(Action.LOCATION_CHANGED_ACTION.toString());
			mContext.sendBroadcast(intent);
		}
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		Log.d(TAG, "onStatusChanged(): " + provider + " status: " + status);
		setsStatus(status);
	}

	@Override
	public void onProviderEnabled(String provider) {
		Log.d(TAG, "onProviderEnabled(): " + provider);
	}

	@Override
	public void onProviderDisabled(String provider) {
		Log.d(TAG, "onProviderDisabled(): " + provider);
	}

}
